/**
 * Created by xiey on 2016/4/19.
 */
var path        = require('path'),
    gulp        = require('gulp'),
    conf        = require('./common/conf'),
    browserSync = require('browser-sync');

function isOnlyChange(event) {
    return event.type === 'changed';
}

gulp.task('watch', ['inject'], function () {

    gulp.watch([path.join(conf.paths.src, '/*.html'), path.join(__dirname, '../package.json')], ['inject-reload']);

    gulp.watch('css/**/*.css', {cwd: conf.paths.src}, function (event) {
        if (isOnlyChange(event)) {
            browserSync.reload(event.path);
        } else {
            gulp.start('inject-reload');
        }
    });

    gulp.watch('js/**/*.js', {cwd: conf.paths.src}, function (event) {
        if (isOnlyChange(event)) {
            gulp.start('scripts-reload');
        } else {
            gulp.start('inject-reload');
        }
    });

    gulp.watch('view/**/*.js', {cwd: conf.paths.src}, function (event) {
        if (isOnlyChange(event)) {
            browserSync.reload(event.path);
        } else {
            gulp.start('inject-reload');
        }
    });

    gulp.watch([path.join(conf.paths.src, '/**/*.html'), '!' + path.join(conf.paths.src, '/*.html')], function (event) {
        browserSync.reload(event.path);
    });
});